<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>二网申请</title>
	<link href="${contextPath}/styles/default.css" rel="stylesheet" type="text/css" />
	<script src="${contextPath}/scripts/jquery/jquery-1.8.3.min.js" type="text/javascript"></script>
	<script src="${contextPath}/scripts/scriptvalidator.js" type="text/javascript"></script>
	<script src="${contextPath}/scripts/common.js" type="text/javascript"></script>
</head>
<body onLoad="doLoad('${finId}','${brandId }')">

<input type="hidden" id="contextPath" value="${contextPath}"/>
<input type="hidden" id="currentPage" value="${currentPage}"/>
<input type="hidden" id="pageData" name="pageData" value="<s:property value='pageData'/>"/>

<div class="navigation">
	    <span><a href="javascript:window.history.back();"><img src="${contextPath}/images/back_nav_btn.png" /></a>
	    <a href="javascript:window.history.back();">返回</a>
	    <a href="javascript:window.history.forward();"><img src="${contextPath}/images/go_nav_btn.png" /></a>
	     <a href="javascript:window.history.forward();">前进</a>
	    <a href="javascript:window.location.reload();"><img src="${contextPath}/images/refresh_nav_btn.png" /></a>
	     <a href="javascript:window.location.reload();">刷新</a>
	    </span>盘库管理&gt;&gt;二网申请
    </div>
<div class="xt_main_right_center">
<form name="searchForm" method="post" id="submitForm" onSubmit="return checkSubmit();"  enctype="multipart/form-data"  action="save.do?markSave=2">
	<input type="hidden" name="backups" id="backups" value='${id}'/>
    <input type="hidden" name="redirectUrl" value='<s:property value='redirectUrl'/>'/>
    <input type="hidden" name="groupIds" id="groupIds" value='${groupIds}'/>
     <input type="hidden" name="id" id="id" value='${id}'/>
     <input type="hidden" name="theIdLong" id="theIdLong" value='${theIdLong}'/>
      <table  width="100%" border="0" cellpadding="0" cellspacing="0"    style="border-color:#CCCCCC;">
        <tr>
          <td class="left-td-width-bg" align="right"><font  color="red" size="2">* </font>4S店：</td>
          <td class="right-td-width-bg-left">
          <select  dataType="Require" msg="请选择销售店 " onChange="doAjaxShop('${finId}','${brandId }')" class="input" name="shopId" id="shopId">
		         <s:iterator value="shops" status="i" id="b">
		          <option <s:if test='shopId==#b.id'>selected="selected"</s:if> value="${b.id }">${b.shopName }</option>
		          </s:iterator>
		      </select>
         </td>
          <td class="left-td-width-bg"><font  color="red" size="2">* </font>渠道：</td>
        
          <td   align="right">
			 <select  dataType="Require" msg="请选择渠道 " onChange="ramCer()" class="input" name="brandId" id="brandId">
		        <option value="">****请选择****</option>
		      </select>
			</td>
          <td class="left-td-width-bg"  align="right"> <font  color="red" size="2">* </font>金融机构：</td>
          <td class="right-td-width-bg-left"  align="right"> 
           <select  dataType="Require" msg="请选择金融机构" onChange="ramCer()" class="select" name="finId" id="finId">
            <option value="">****请选择****</option>
          </select>
           
        </tr>
        <tr>
          <td class="left-td-width-bg"  align="right">申请人：</td>
          <td class="right-td-width-bg-left" >${user.name }</td>
          <td class="left-td-width-bg" >申请单号：</td>
          <td  class="right-td-width-bg-left"  align="right">${Num }</td>
          <td class="left-td-width-bg"  align="right">申请时间：</td>
          <td class="right-td-width-bg-left"   align="right">&nbsp;</td>
        </tr>
        <tr>
          <td class="left-td-width-bg"  align="right">附件上传：</td>
          <td colspan="5"  >
			<input name="file"  type="file" id="file" size="16" style="width: 200px" />
					<s:iterator value="attachs" >
					<div style="display: inline">
						<a href="${contextPath}/attach/download.do?id=<s:property value="id"/>" ><s:property value="name"/></a>&nbsp;<img name="img_del" id="<s:property value="id"/>" src="${contextPath}/images/del.gif" alt="删除" width="16" height="18" border="0" onclick="javascript:del(this,<s:property value="id"/>)" />
					</div>
					</s:iterator> 
					<s:iterator value="attachs" >
					<div style="display: inline">
						<a href="${contextPath}/attach/download.do?id=<s:property value="id"/>" ><s:property value="name"/></a>&nbsp;<img name="img_del" id="<s:property value="id"/>" src="${contextPath}/images/del.gif" alt="删除" width="16" height="18" border="0" onclick="javascript:del(this,<s:property value="id"/>)" />
					</div>
				</s:iterator> 
		  </td>
        </tr>
         <tr>
          <td class="left-td-width-bg"  align="right">申请理由：</td>
          <td colspan="5"  >
			 <textarea name="msg" cols="30" rows="5" id="textfield"></textarea>
		  </td>
        </tr>
        <tr>
           <td class="left-td-width-bg"  align="right">申请列表：</td>
          <td colspan="5"  >
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td colspan="10" align="right" ><label>
                <input type="button" name="button3" id="button3" onClick="openCer()" value="选择合格证">
              </label></td>
            </tr>
            <tr>
               
              <td class="top-td-width-bg">序号</td>
              <td class="top-td-width-bg">合格证号</td>
              <td class="top-td-width-bg">车架号</td>
              <td class="top-td-width-bg">车型</td>
              <td class="top-td-width-bg">发动机号</td>
              <td class="top-td-width-bg">供货价</td>
              <td class="top-td-width-bg">回库地址</td> 
              <td class="top-td-width-bg">现存二网地址</td>
              <td class="top-td-width-bg">二网地址</td>
              <td class="top-td-width-bg">操作</td>
             
            </tr>
            <s:iterator value="certifications" status="i" id="c">
            <tr>
              <input type="hidden" name="ctId" value="${c.id }"/>
              <td class="down-center-td-width-bg">${i.count }</td>
              <td class="down-center-td-width-bg">${c.certificationNum }</td>
              <td class="down-center-td-width-bg">${c.chassis }</td>
              <td class="down-center-td-width-bg">${c.motorcycleType }</td>
              <td class="down-center-td-width-bg">${c.engine }</td>
              <td class="down-center-td-width-bg"><s:property value="formatAmount(#c.supplyPrice)"/></td>
              <td class="down-center-td-width-bg">${c.shop.shopName }</td>
              <td class="down-center-td-width-bg">${c.copyShow }</td>
              <td class="down-center-td-width-bg">
             	 <input name="thew" dataType="Require" msg="请输入二网地址 "  type="text" value="${c.theAddress }">
               </td>
               <td class="down-center-td-width-bg"><a href="javascript:deleteIDs(${c.id })" title="删除"><img src="${contextPath}/images/del.gif" alt="删除" width="16" height="13" border="0" /></a></td>
      		</td>
            </tr>
            </s:iterator>
          </table></td>
        </tr>
         <tr>
      <td class="down-center-td-width-bg"  colspan="11" align="right">
		 <s:if test="certifications.size() <= 0">
            	<font  color="red" size="2">没有可申请的数据，请重新选择</font>
            </s:if>
</td>
    </tr>
        <tr>
          <td colspan="6" align="center" valign="middle">
          
          <input type="submit" name="button" class="btn_temporary" id="button" value="">
          <input type="button" name="button2" onClick="save()" class="btn_submit" value="" id="button2" >
            <input type="button" name="button2" class="btn_rest" onClick="clearForm(this.form);return false;" id="button2" ></td>
        </tr>
      </table>
      
      <input type="hidden" id="allData" name="allData" value="<s:property value='allData'/>"/>
      	  <s:if test="workflow!=null&&workflow.operates!=null">
		<br/>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
		  <tr>
		    <td height="25" align="left"><font color="#0066CC"><strong>流程操作记录：</strong></font></td>
		    <td height="25" align="right">
		    </td>
		  </tr>
		</table> 
		<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
		  <tr> 
		    <td width="3%" class="top-td-width-bg">序号</td>
		    <td width="20%" class="top-td-width-bg">操作人</td>
		    <td width="20%" class="top-td-width-bg">操作类型</td>
		    <td width="40%" class="top-td-width-bg">描述</td>
		    <td width="17%" class="top-td-width-bg">操作时间</td>
		  </tr>
		 <s:iterator value="workflow.operates" status="stx" >
			<tr height="26" align="center" bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='#FCFDEE';" onMouseOut="javascript:this.bgColor='#FFFFFF';"> 
			 	<td align="center"><s:property value="#stx.index+1"/></td>
			    <td align="center"><s:property value="operater.realName"/></td>
			    <td align="center"><s:property value="type.name"/></td>
			    <td align="center"><s:property value="description"/></td>
			    <td align="center"><s:property value="operateDate"/></td>
			</tr>
		</s:iterator>
		</table>
		<br/>
	</s:if>
</form> 
</div>
<br/><br/><br/><br/><br/>
		<div><jsp:include page="/include/footer.inc"></jsp:include></div>
<script  language='javascript'>
function cul(){
	var allData = new Array();
	var a = false;
	allData = JSON.parse($("#allData").val());
	
	for(var i=0;i<allData.length;i++){
		var obj = allData[i];
		if(obj.theAddress != null && obj.theAddress != ""){
			a = true;
		};
	}
	if(a == false){
		alert("请填写二网地址");
		return false;
	}
	return a;
}
function jumpPage(page) {
	var searchForm = document.searchForm;
	searchForm.action = "<s:property value='contextPath'/>/cs/ct/choose.do?currentPage=" + page+ "&state=2" ;
	searchForm.submit();
}
function checkSubmit(){
 	var staffForm = document.searchForm;
	if(!Validator.Validate(staffForm, 3)){
		return false;
	}
	var exp = $("#allData").val();
	if (exp=="" || exp== null)
	{
	    alert("请重新选择合格证");
	    return false;
	}
	return true;
 } 
 function save(){
	 if(checkSubmit()){
		 var searchForm = document.searchForm;
		 
			searchForm.action = "save.do?markSave=1";
			searchForm.submit(); 
	 }
	
 }
 
 function ramCer(){
	 $("#allData").val("");
 }

function openCer(){
	var id = $("#id").val();
	$("#id").val("");
	if($("#finId").val() && $("#brandId").val() && $("#shopId").val()){
		var ids = document.getElementById("groupIds").value;
		var url = $("#contextPath").val() + "/cs/ct/openChoose.do?currentPage=" + $("#currentPage").val() + "&state=2" + "&finId=" + $("#finId").val() + "&brandId=" + $("#brandId").val() + "&shopId=" + $("#shopId").val() + "&groupIds=" + ids  + "&id=" + id ;
		
		window.open(url,
				"newwindow",
				"height=450, width=700, toolbar =no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
	}else{
		
		alert("请把条件输入完整");
	}
	
}
function deleteIDs(el){
	if(typeof JSON == 'undefined'){//判断是否兼容 json
        $('head').append($("<script type='text/javascript' src='${contextPath}/scripts/json/json2.js'>"));
	}
	var id_all =document.getElementById("groupIds").value.split(",");
	var data_id = "";
	for(var i=0;i<id_all.length-1;i++){
		if(id_all[i] == el){
		}else{
			data_id += id_all[i]+",";
		}
	}
	var allData = new Array();
	if($("#allData").val()){
		
		allData = JSON.parse($("#allData").val());
	}

	$(allData).each(function(index, obj){
		if(obj.id == el){
			allData.splice(index,1);
		}
		
	});
	//alert(JSON.stringify(allData));
	document.getElementById("groupIds").value=data_id;
	$("#allData").val(JSON.stringify(allData));
	addChoose(data_id);
}

function addChoose(valIDS){
	$("#id").val("");
		document.getElementById("groupIds").value=valIDS;
		var url = $("#contextPath").val() + "/cs/ct/addChoose.do?currentPage=" + $("#currentPage").val() + "&state=2" + "&finId=" + $("#finId").val() + "&brandId=" + $("#brandId").val() + "&shopId=" + $("#shopId").val()  + "&groupIds=" + valIDS ;
		
		submitForm.action = url;
		
		submitForm.submit();
		
}

$(function(){
	 
	
	// 加载页面时，判断文本框的值，动态去选中或取消选中同行的checkbox
	$("[name='thew']").blur(function(){
		if(typeof JSON == 'undefined'){//判断是否兼容 json
            $('head').append($("<script type='text/javascript' src='${contextPath}/scripts/json/json2.js'>"));
		}
		var id = $(this).parent().parent().find("input[name='ctId']").val();
		
		var address = $(this).val();
		
		var allData = new Array();
		
		if($("#allData").val()){
			
			allData = JSON.parse($("#allData").val());
		}
		
		$(allData).each(function(index, obj){
			
			if(obj.id == id){
				obj.theAddress = address;
				
			}
		});
		
		$("#allData").val(JSON.stringify(allData));
	});
	
	
});

function del(e,id){
	if(confirm("附件操作不可恢复,确认删除吗?")){
		var f=false;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url: "<s:property value='contextPath'/>/attach/del.do?id="+id,
			  dataType: "json",
			  success: function(data) {
					  	f = data.flag;
					  	if(f){
					  		//alert("删除成功");
					  		$(e).parent().remove();
					  	}
					},
				error:function(data){
				}
			}); 
		
	}
	
}
function doLoad(fianlaId,brandId){
	var s = document.readyState;
	var arrayObj = new Array();//创建一个数组
	var arrayBrand = new Array();//创建一个数组
	if(s=="complete"){
		var obj = document.getElementById("shopId").value;
		doAjaxTest(obj);
		doAjaxBrandTest(obj);
		
		
			if(null != fianlaId && brandId != null){
				arrayObj = document.getElementById("finId");
				arrayBrand = document.getElementById("brandId");
				for(var i=0;i<arrayObj.length;i++){
					if(fianlaId==arrayObj.options[i].value){
						arrayObj.options[i].selected=true;
					}
					
				}
				for(var i=0;i<arrayBrand.length;i++){
					if(brandId==arrayBrand.options[i].value){
						arrayBrand.options[i].selected=true;
					}
					
				}
			}
			
	}
}
function doAjaxShop(fianlaId,brandId){
	var s = document.readyState;
	var arrayObj = new Array();//创建一个数组
	var arrayBrand = new Array();//创建一个数组
	if(s=="complete"){
		var obj = document.getElementById("shopId").value;
		doAjaxTest(obj);
		doAjaxBrandTest(obj);
		ramCer();
		
			if(null != fianlaId && brandId != null){
				arrayObj = document.getElementById("finId");
				arrayBrand = document.getElementById("brandId");
				for(var i=0;i<arrayObj.length;i++){
					if(fianlaId==arrayObj.options[i].value){
						arrayObj.options[i].selected=true;
					}
					
				}
				for(var i=0;i<arrayBrand.length;i++){
					if(brandId==arrayBrand.options[i].value){
						arrayBrand.options[i].selected=true;
					}
					
				}
			}
			
	}
}

function doAjaxTest(i){
	if(i != null){
		var select=document.getElementById("finId");
	    select.length=1;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url:"<s:property value='contextPath'/>/cs/ct/shopFinal.do?shopId="+i,
			  dataType: "json",
			  success: function(data) {
				  if(data !=null){
					  var obj=document.getElementById("finId");
					  obj.options.length=1;
					  var a=new Array();
					  var b=new Array();  
					  a = data.toString().split(",");
					  for(var i=0;i<a.length;i++){
						  b = a[i].split(":");
						  obj.options.add(new Option(b[1],b[0]));
					  }
				  }
				  
			  }
			});
	}
	
}
function doAjaxBrandTest(i){
	if(i != null){
		var select=document.getElementById("brandId");
	    select.length=1;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url:"<s:property value='contextPath'/>/cs/ct/detailsBrand.do?shopId="+i,
			  dataType: "json",
			  success: function(data) {
				  if(data !=null){
					  var obj=document.getElementById("brandId");
					  obj.options.length=1;
					  var a=new Array();
					  var b=new Array();  
					  a = data.toString().split(",");
					  for(var i=0;i<a.length;i++){
						  b = a[i].split(":");
						  obj.options.add(new Option(b[1],b[0]));
					  }
				  }
				  
			  }
			});
	}
	
}
</script>
 
</body>
</html>